<template>
  <div>
    <div class="searchBox">
      <div class="searchInputs">
        <el-input class="searchInput" v-model="key" style="width: 240px" placeholder="按关键字搜索" />
      </div>
      <el-button type="primary" @click="refresh">搜索</el-button>
      <el-button type="primary" @click="edit()">新增</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData" style="width: 100%;">
        <el-table-column fixed prop="userName" label="用户名" width="150" />
        <el-table-column prop="nickName" label="昵称" width="120" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="sex" label="性别" width="120">
          <template #default="scope">
            <span style="margin-left: 10px">{{ scope.row.sex == 1 ? '男' : '女' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" />
        <el-table-column fixed="right" label="Operations" width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="edit(scope.row)">修改</el-button>
            <el-button link type="primary" size="small" @click="delet(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination 
      layout="prev, pager, next" 
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[100, 200, 300, 400]"
      @size-change="getList"
      @current-change="getList"
    :total="total" />
    <el-dialog v-model="dialogFormVisible" title="编辑用户" width="500">
      <el-form :model="form" ref="editForm" :rules="rules">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" />
        </el-form-item>
        <el-form-item label="头像" prop="img">
          <el-upload
            class="avatar-uploader"
            :action="baseURL + '/upload'"
            :headers="headers"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.img" :src="form.img" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon">+</el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="男" value="1" />
            <el-option label="女" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="save">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import md5 from 'js-md5'
import { ElMessageBox } from 'element-plus';
let form = {
  userName:'',//用户名
  passWord:'',//密码
  nickName:'',
  name:'',
  sex:'',
  phone:'',
  img:''
}
export default {
  name: 'userList',
  data(){
    return {
      baseURL: 'http://localhost:3000',
      tableData:[],
      key:'',
      total:0,
      pageNum:1,
      pageSize: 10,
      dialogFormVisible:false,
      form:Object.assign(form),
      headers:{
        token:sessionStorage.getItem('token')
      },
      rules:{
        userName: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    handleAvatarSuccess(event){
      console.log(22,event)
      this.form.img = this.baseURL + event.path
    },
    beforeAvatarUpload(file){
      console.log(11,file)
    },
    refresh(){
      this.pageNum = 1
      this.getList()
    },
    async delet(row){
      ElMessageBox.confirm('确定删除这个用户', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        callback: async action => {
          if(action == 'confirm'){
            let res = await this.$delete('/user',{ id:row.id })
            if(!res.errorMsg) this.refresh()
          }
        }
      })
    },
    async edit(row){
      this.form = row ? row : Object.assign(form)
      this.dialogFormVisible = true
    },
    async save(){
      this.$refs.editForm.validate(async valid => {
        if(valid){
          if(this.form.id) {//修改
            let res = this.$put('/user',this.form)
            if(!res.errorMsg) {
              this.dialogFormVisible = false
              this.refresh()
            }
          }else{//新增
            this.form.passWord = md5('123456')
            let res = this.$post('/user',this.form)
            if(!res.errorMsg) {
              this.dialogFormVisible = false
              this.refresh()
            }
          }
          
        }
      })
    },
    async getList(){
      let res = await this.$get('/user/list',{
        key:this.key,
        pageNum:this.pageNum,
        pageSize:this.pageSize
      })
      this.total = parseInt(res.total)
      this.tableData = res.rows
    }
  },
  mounted(){
    this.getList()
    // this.$post('/user',{
    //   userName:'zhans',
    //   passWord:'12312313'
    // })
  }
}
</script>
<style scoped>
.searchBox {
  margin-bottom: 20px;
  display: flex;
}

.searchInputs {
  margin-right: 16px;
}

.searchInput {
  margin-right: 16px;
}

.table {
  width: 100%;
}


.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}


.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

</style>